package com.JobFind.DAO.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.JobFind.DAO.IRecruitmentDAO;
import com.JobFind.model.TbRecruitment;
import com.JobFind.model.TbDeliver;

@Component
public class RecruitmentDAO implements IRecruitmentDAO{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@SuppressWarnings("unchecked")
	public List<TbRecruitment> getJobList(Integer userId,String sQuery) throws Exception {
		List<TbRecruitment> result = null;
		Session session = sessionFactory.getCurrentSession();
		String hql="from TbRecruitment where isDeleted=? and recruitmentId not in(select tbRecruitment.recruitmentId from TbDeliver where tbUser.userId=?) and RecruitmentAreNum < RecruitmentNum"+ sQuery;
		Query query = session.createQuery(hql);
		query.setInteger(0, 0);
		query.setInteger(1, userId);
		//query.setFirstResult(startSet);
		//query.setMaxResults(endSet);
		result = query.list();
		return result;
	}

	public TbRecruitment getRecruitmentInfoById(Integer recruitmentId)
			throws Exception {
		TbRecruitment result = null;
		Session session = sessionFactory.getCurrentSession();
		String hql = "from TbRecruitment where recruitmentId=? and isDeleted=?";
		Query query = session.createQuery(hql);
		query.setInteger(0, recruitmentId);
		query.setInteger(1, 0);
		result = (TbRecruitment) query.list().get(0);
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<TbRecruitment> getRecruitInfoByCompanyId(Integer companyId)
			throws Exception {
		List<TbRecruitment> result = null;
		Session session = sessionFactory.getCurrentSession();
		String hql = "from TbRecruitment where tbCompany.companyId = ? and isDeleted=?";
		Query query = session.createQuery(hql);
		query.setInteger(0, companyId);
		query.setInteger(1, 0);
		result = query.list();
		return result;
	}

	public Integer updateSomeRecritInfo(Integer recruitmentId,
			String recruitmentPosition, String recruitmentInfo,
			String recruitmentRemarks) throws Exception {
		Integer result = 1;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "update TbRecruitment set recruitmentPosition = ?,recruitmentInfo = ?, recruitmentRemarks = ? where recruitmentId = ?";
			Query query = session.createQuery(hql);
			query.setString(0, recruitmentPosition);
			query.setString(1, recruitmentInfo);
			query.setString(2, recruitmentRemarks);
			query.setInteger(3, recruitmentId);
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	public Integer deleJob(Integer recrimentId) throws Exception {
		Integer result = 1;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="update TbRecruitment set isDeleted=? where recruitmentId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, 1);
			query.setInteger(1, recrimentId);
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			result = 1;
		}
		return result;
	}

	public Integer addRecruitmnet(TbRecruitment recruitmentBean)
			throws Exception {
		Integer result = 1;
		try {
			Session session = sessionFactory.getCurrentSession();
			session.save(recruitmentBean);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	public Integer upRecritmentAreNum(Integer recruitmentId) throws Exception {
		Integer result = 1;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="update TbRecruitment set recruitmentAreNum=(recruitmentAreNum+1) where recruitmentId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, recruitmentId);
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			result = 1;
		}
		return result;
	}

	public String getCompanyNameByRecId(Integer recruitmentId) throws Exception {
		String result = "";
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="select tbCompany.companyName from TbRecruitment where recruitmentId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, recruitmentId);
			result = query.uniqueResult().toString();
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
		}
		return result;
	}

	public Integer getCompanyIdByRecId(Integer recruitmentId) throws Exception {
		Integer result = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="select tbCompany.companyId from TbRecruitment where recruitmentId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, recruitmentId);
			result = (Integer)query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
	
}
